<?php

	fta_registerModules(
		array(
			"FTAORM/FTAORM",
		)
	);
	
class FTAORMCMSMultipleKeyValuePair extends FTAORM  {

	const TABLE_SEQUENCE_NAME = 'multiplekeyvaluepairsequence_seq';
	function __construct($zdb) {
		
		parent::__construct($zdb);			
			$this->primaryKey = "id";
			$this->tableFields = array(
		  		'documentcode',
		  		'key',
		  		'value',
		  		'elementid'
			);//excluding the pk..
									
			$this->tableFieldTypes = array(
		  		'documentcode'	=>'number',		
		  		'documentcode' => 'string',
		  		'value' => 'string',
		  		'elementid'		=>'number'					
			);
						
			$this->tableRequiredFields = array(
				'documentcode',
				'elementid'				
			);
			
			$this->tableName = "multiplekeyvaluepairs";	
			
			$this->tableRelationships = array();

			$this->tableRelationshipClasses = array();	
	
		}	

		static function getAllKeyValuePairsForDocumentCodeAndElementId($documentCode, $elementId, $zdb){
			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(\Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName))
                ->where('documentcode = ?', $documentCode)
            	->where('elementid = ?', $elementId);
            	
			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);			
		}
		
		
		/**
		 * Gets all kvp for the given document code .. primarily used for cloning
		 * @param unknown_type $documentCode
		 * @param unknown_type $zdb
		 */
		static function getAllKeyValuePairsForDocumentCode($documentCode, $zdb){
			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(\Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName))
                ->where('documentcode = ?', $documentCode);
                            	
			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);			
		}
		
		
		
		static function getNewPrimaryKey(Zend_Db_Adapter_Abstract $zdb){			
			$zdb->query('UPDATE ' . static::$TABLE_SEQUENCE_NAME .' SET id=LAST_INSERT_ID(id+1)');
			$id = $zdb->lastInsertId();
			return $id;
		}	
}